$(function () {
  // wzERP.Contract.init();
  // wzERP.Contract.turnPage();//分页
  wzERP.Common.inlineDate('#date1', '#date2');  //联动日期
});

var orderDataSet = {'0': '线上订单', '1': '线下订单', '': '全部'}; //订单数据字典
//表单模板数据请求
demo(1)
// 获取当前页面内容
function demo(curr, num, searchKey, startDate, endDate, orderType, isBlance,
    busType) {
  //curr:当前页
  //num:每页显示条数
  //searchKey:搜索内容
  //startDate:开始时间
  //endDate:结束时间
  $.get('/finance/erpAccountReceivable/queryPage', {
    current: curr || 1,
    pageSize: num || 10,
    custName: searchKey,
    startDate: startDate,
    endDate: endDate,
    orderType: orderType,
    isBlance: isBlance,
    businessTypeId: busType
  }, function (content) {
    //获取内容显示到模板上
    $('#LinelistAreaData').html(template('LinelistArea', content.data))
    // 分页
    layui.use(['laypage', 'layer'], function () {
      var laypage = layui.laypage;
      laypage({
        cont: 'turn-page',
        curr: curr || 1,
        pages: content.data.pages,
        skin: '#457fe6',
        skip: true,
        jump: function (obj, first) {
          var num = $('#LinenumberOf select').val()
          if (!first) {
            demo(obj.curr, num);
          }
        }
      });
    });
    var listData = content.data;
    //绑定
    $('#LinelistAreaData').on('click', 'tr td:nth-child(1)', function () {
      $('#LineDetailsToModal').modal({
        closable: false,
        transition: 'fade'
      }).modal('show');
      if (listData && listData.records && listData.records.length > 0) {
        var obj = listData.records[$(this).parent().index()]
        $('#custName').html(obj.custName)
        $('#orderNo').html(obj.orderCode)
        $('#ysDate').html(obj.billDatetime)
        $('#ysNo').html(obj.accountReceivableCode)
        $('#orderMType').html(orderDataSet[obj.orderType])
        $('#busTpye').html(obj.businessTypeName)
        $('#yszk').html(obj.receiveAmount)
        $('#ysje').html(obj.receivedAmount)
        $('#ye').html(obj.receivableBalance)
        $('#tz').html(obj.accountRegulation)
      }
    })
  })
}
//查询客户列表
querCustList();
function querCustList() {
  $.get('/finance/erpAccountReceivable/queryCustGroupList', {},
      function (result) {
        var content = result.data;
        var objResult = [];

        for (var i = 0; i < content.length; i++) {
          var obj = content[i];
          var newObj = {};
          newObj.title = obj.custName;
          objResult.push(newObj);
        }
        $('.ui.search')
        .search({
          source: objResult,
          searchFullText: false
        })

      })
}
var busDataSet = {'all': '全部'};//业务类型数据字典
//查询业务类型
querBusList();
function querBusList() {
  $.get(
      '/finance/erpAccountReceivable/querydictionDetailListByCode?code=receivablesToS',
      {}, function (result) {
        var content = result.data;
        var objResult = [];
        //测试数据
        content = [{id: 111, dictionaryDetailName: '测试数据'}]
        for (var i = 0; i < content.length; i++) {
          var obj = content[i];
          busDataSet[obj.id] = obj.dictionaryDetailName
          $("#status").append(
              "<option value='" + obj.id + "'>" + obj.dictionaryDetailName
              + "</option>")
        }

      })
}

// 每页显示条数
$('#LinenumberOf ').on('click', function () {
  //获取当前页数
  var page = $('.layui-laypage-skip').val()
  // 获取表单时间
  var content = $("#LinesearchForm").serialize()
  //使用方法分割取出
  var contentS = content.split('&')
  var obj = {}
  for (var i = 0; i < contentS.length; i++) {
    var con = contentS[i].toString()
    var son = con.split('=')
    obj[son[0]] = son[1]
  }
  //获取当前每页应该显示条数
  var num = $('#LinenumberOf select').val()
  //获取搜索框内容
  var searchKey = $('#searchKey').val()
  //获取分割好的起止时间
  var startDate = obj.startDate
  var endDate = obj.endDate
  var orderType = $('#orderType').val();
  var isBlance = $('#isBlance').is(':checked') ? '1' : '0' //是否有余额
  var busType = $('#status').val();
  if (busType == 'all') {
    busType = ''
  }
  if (orderType == '2') {
    orderType = ''
  }
  demo(page, num, searchKey, startDate, endDate, orderType, isBlance, busType)
})

//查询业务
$("#LinetimeA").on('click', function () {
  // 获取表单时间
  var content = $("#LinesearchForm").serialize()
  //使用方法分割取出
  var contentS = content.split('&')
  var obj = {}
  for (var i = 0; i < contentS.length; i++) {
    var con = contentS[i].toString()
    var son = con.split('=')
    obj[son[0]] = son[1]
  }
  //获取当前每页应该显示条数
  var num = $('#LinenumberOf select').val()
  //获取客户搜索框内容
  var searchKey = $('#searchKey').val()
  //获取分割好的起止时间
  var startDate = obj.startDate
  var endDate = obj.endDate
  var orderType = $('#orderType').val()//订单类型
  var isBlance = $('#isBlance').is(':checked') ? '1' : '0' //是否有余额
  var busType = $('#status').val()//业务类型
  if (busType == 'all') {
    busType = ''
  }
  if (orderType == '2') {
    orderType = ''
  }
  //获取搜索内容
  query(1, num, searchKey, startDate, endDate, orderType, isBlance, busType)
  function query(curr, num, searchKey, startDate, endDate, orderType, isBlance,
      busType) {
    $.get('/finance/erpAccountReceivable/queryPage', {
      current: curr || 1,
      pageSize: num || 10,
      custName: searchKey,
      startDate: startDate,
      endDate: endDate,
      orderType: orderType,
      isBlance: isBlance,
      businessTypeId: busType
    }, function (content) {
      //获取内容显示到模板上
      $('#LinelistAreaData').html(template('LinelistArea', content.data))
      // 分页
      layui.use(['laypage', 'layer'], function () {
        var laypage = layui.laypage;
        laypage({
          cont: 'turn-page',
          curr: curr || 1,
          pages: content.data.pages,
          skin: '#457fe6',
          skip: true,
          jump: function (obj, first) {
            var num = $('#LinenumberOf select').val()
            if (!first) {
              query(obj.curr, num, searchKey, startDate, endDate);
            }
          }
        });
      });
    })
  }

  return false
})

$('#LineService').on('click', function () {
  $('#LineSelectionService').css('display', 'block')
})
$('#LineDisappear').on('click', function () {
  $('#LineSelectionService').css('display', 'none')
})
$('#LineClose').on('click', function () {
  $('#LineSelectionService').css('display', 'none')
})

//订单详情
$('#LinelistAreaData').on('click', '#LineDetails', function () {
  $('#LineDetailsToModal').modal({
    closable: false,
    transition: 'fade'
  }).modal('show')
})

//编辑订单
$('#LinelistAreaData').on('click', '#LineEdit', function () {
  $('#LineAddToModal').modal({
    closable: false,
    transition: 'fade'
  }).modal('show')
})

//审核订单
$('#LinelistAreaData').on('click', '#LineExamine', function () {
  $('#LineExamineToModal').modal({
    closable: false,
    transition: 'fade'
  }).modal('show')
})

//分单
$('#LinelistAreaData').on('click', '#LinePolicy', function () {
  $('#LinePolicyToModal').modal({
    closable: false,
    transition: 'fade'
  }).modal('show')
})

//立案
$('#LinelistAreaData').on('click', '#LineFiling', function () {
  $('#LineFilingToModal').modal({
    closable: false,
    transition: 'fade'
  }).modal('show')
})

